"""
环境变量： 名称：DISCUZ_DSU   如：网站域名(含协议头)##Cookies
多账号新建变量或者用 & 分开

定时：一天1-2次

更新日志：
2024-02-04

cron: 1 0 * * *
const $ = new Env("Discuz论坛签到");
"""

import os
import re

import requests

headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cookie': '__cfduid=',
    'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
    'Referer': 'https://link-ai.tech/home',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36'
}

if __name__ == '__main__':
    users = os.getenv('DISCUZ_DSU')
    if users is None:
        print('请先设置 DISCUZ_DSU 变量 格式：域名##Cookies')
        exit()

    users = users.split('&')
    for user in users:
        userArr = user.split('##')
        if len(userArr) != 2:
            continue

        res = requests.get(url=userArr[0] + '/plugin.php?id=dsu_paulsign:sign', headers={
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Cookie': userArr[1],
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36'
        }).text
        formHash = re.findall(r'.*formhash=(\w+).*', res)
        if not formHash:
            formHash = re.findall(r'<input type=\"hidden\" name=\"formhash\" value=\"(.*?)\" />', res)

        formHash = formHash[0]

        res = requests.get(url=userArr[0] +
                               '/plugin.php?id=dsu_paulsign:sign&operation=qiandao&formhash=' + formHash + '&qdmode=2&fastreply=0&qdxq=ym&infloat=yes&handlekey=dsu_paulsign&inajax=1&ajaxtarget=fwin_content_dsu_paulsign',
                           headers={
                               'Accept-Encoding': 'gzip, deflate',
                               'Accept-Language': 'zh-CN,zh;q=0.9',
                               'Cookie': userArr[1],
                               'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36'
                           }).text

        print(res)
